<?php

uses('sanitize');

class CategoriesController extends AppController {

	var $name = 'Categories';
	var $helpers = array('Html2', 'Form', 'Ajax');
	var $components = array('RequestHandler'); 

//action dealers

/*	//ajax - required in product add - POST
	function ajaxadd(){
		$this->set('title_error', 'Le titre de la categorie ne doit pas être vide.');
		if(!empty($this->data)){
			$san = new Sanitize();
			$this->data['Category']['title'] = $san->paranoid($this->data['Category']['title'], array(' ', '-', '+', '.'));
			$this->data['Category']['description'] = $san->sql($this->data['Category']['description']);
			
			$result = $this->Category->findByTitle($this->data['Category']['title']);
			if(!empty($result))
			{
				$this->Category->invalidate('title');
				$this->set('title_error', 'Une categorie avec ce titre existe déjà.');
			} else {
				$this->cleanUpFields();
				if($this->Category->save($this->data)) {
					//Acl
					$aco = new Aco();
					$id = $this->Category->id;
					$aco->create(0, 0, 'category-'.$id);
					$this->Acl->allow('dealers', 'category-'.$id, '*');
					$this->Acl->allow('admins', 'category-'.$id, '*');
					
					$this->Session->setFlash('La categorie a été enregistré');
					$this->set('closeajax', true);
				} else {
					$this->Session->setFlash('Merci de corriger les erreurs suivantes.');
				}
			}
		}
		$this->render('ajaxadd', 'ajax');
	}*/
	function admin_index() {
		$this->pageTitle = 'Admin Categories';
		$this->set('categories', $this->Category->findAll(null,null, 'position ASC', null, null, 0));
	}
	
	function admin_add() {
		$this->pageTitle = 'Add Category';
		
		if($this->RequestHandler->isPost()) {
			$san = new Sanitize();
			$this->data['Category']['title'] = $san->paranoid($this->data['Category']['title'], array(' ', '.', '+', '-', ':', '*', '$', '^', '_', '[', ']', '(', ')'));
			$this->data['Category']['slug'] = $san->paranoid($this->data['Category']['slug'], array('-'));

			$this->cleanUpFields();
			if($this->Category->save($this->data)) {
								
				$this->Session->setFlash('The category has been saved.');
				$this->redirect('/admin/categories');
			} else {
				$this->Session->setFlash('Please correct error below.');
			}
		}
	}

	function admin_edit($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Category');
			$this->redirect('/admin/categories/index');
		}
		
		if($this->RequestHandler->isPost()) {
			$san = new Sanitize();
			$this->data['Category']['title'] = $san->paranoid($this->data['Category']['title'], array(' ', '.', '+', '-', ':', '*', '$', '^', '_', '[', ']', '(', ')'));
			$this->data['Category']['slug'] = $san->paranoid($this->data['Category']['slug'], array('-'));
	
			$this->cleanUpFields();
			if($this->Category->save($this->data)) {
				$this->Session->setFlash('The category has been updated.');
				$this->redirect('/admin/categories/index');
			} else {
				$this->Session->setFlash('Please correct error below.');
			}	
		}else{
			$this->data = $this->Category->read(null, $id);
		}
		
		$this->pageTitle = 'Edit '.$this->data['Category']['title'].' category';
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Category');
			$this->redirect('/admin/categories/index');
		}
		if($this->RequestHandler->isPost()){
			$this->Category->del($id);
				//pages with this cat_id isn't reset but new cat can't take this id
				$this->Session->setFlash('The Category with id : '.$id.' has been deleted.');
				$this->redirect('/admin/categories');
		}else{
			$this->Session->setFlash('Error');
			$this->redirect('/admin/categories');
			//error log
		}
	}
	
	function admin_order(){
		foreach ($this->params['form']['category_list'] as $order => $id){
			$data['category_list']['order'] = $order;
			$this->Category->id = $id;
			if($this->Category->saveField('position', $order)) {
				//we have success!
			} else {
				//deal with possible errors!
			}
		}
		$this->autoRender=false;
	}


}
?>
